//index.js
//获取应用实例
const app = getApp()
const voice = require("../../data/index.js");
Page({
  data: {
    motto: '即将进入语音圈...',
    progress:0,
    userInfo: {},
    hasUserInfo: false,
    voice:voice.data,
    voice_index:1,
    animationData: {},
    animate_timer:null,
    speed:0,
    play_icon:'icon-play'
  },
  startGo: function () {
    var animation = wx.createAnimation({
      timingFunction: 'linear',
    })

    this.animation = animation;
    if (this.data.animate_timer != null) {
      wx.pauseBackgroundAudio();
      clearInterval(this.data.animate_timer);
     
      this.setData({
        animate_timer: null,
        play_icon:'icon-play'
      })
      return false;
    }else{
      wx.playBackgroundAudio({
        dataUrl: 'http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E06DCBDC9AB7C49FD713D632D313AC4858BACB8DDD29067D3C601481D36E62053BF8DFEAF74C0A5CCFADD6471160CAF3E6A&fromtag=46',
        title: '',
        coverImgUrl: 'http://y.gtimg.cn/music/photo_new/T002R300x300M000003rsKF44GyaSk.jpg?max_age=2592000'
      })
      var timer = setInterval(function () {
        animation.rotate(this.data.speed).step();
        this.setData({
          animationData: animation.export(),
          speed: this.data.speed + 1,
        })
      }.bind(this), 30);

      this.setData({
        play_icon: 'icon-pause',
        animate_timer: timer
      });
    }
  
   
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      //url: '../logs/logs'
    })
  },
  getCurrentVoice(){
    return this.data.voice[this.data.voice_index];
  },
  onLoad: function () {
    this.setData({
      voiceData: this.getCurrentVoice()
    });
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    }else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo;
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
    wx.onBackgroundAudioPlay(function () {
      this.setData({
        play_icon: 'icon-play'
      })
    });
    wx.onBackgroundAudioPause(function () {
      this.setData({
        play_icon: 'icon-pause'
      })
    });
  },
})